package com.dingding.service.user.facade;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.exception.bussiness.BizExpEnum;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.oss.upload.ImageUpload;
import com.dingding.facade.user.dto.adduser.AddUserAttrFactory;
import com.dingding.facade.user.dto.adduser.AddUserAttribute;
import com.dingding.facade.user.dto.common.driver.ReqDriverAuthImageDTO;
import com.dingding.facade.user.dto.common.driver.ResDriverAuthImageDTO;
import com.dingding.facade.user.facade.IUserOperateFacade;
import com.dingding.service.user.core.dao.user.IUserOperateDao;

/**
 * @Description:用户操作的接口
 * @author fengshuonan
 * @date 2016年9月13日 下午4:34:59
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("userOperateFacade")
public class UserOperateFacadeImpl implements IUserOperateFacade {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "userOperateDao")
	private IUserOperateDao userOperateDao;

	@Override
	public UserLogin judgeUserIsExist(String telephone) {
		UserLogin ul = this.userOperateDao.getByTelephone(telephone);
		if (ul != null) {
			return ul;
		} else {
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public UserLogin addUser(AddUserAttribute addUserAttribute) {
		return this.userOperateDao.addUser(addUserAttribute);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public UserLogin getOrAddUserByTravelAgc(String telephone) {

		// 判断该手机号注册没
		UserLogin ul = this.judgeUserIsExist(telephone);

		try {
			// 如果手机号没注册过，则注册一个
			if (ul == null) {
				ul = this.addUser(AddUserAttrFactory.byTravelAgc(telephone, "empty"));// 没有注册来源的业务
			}
			return ul;
		} catch (Exception e) {
			log.error("获取登录信息出现错误！UserOperateFacadeImpl：getOrAddUser()", e);
			return null;
		}
	}

	@Override
	public ResDriverAuthImageDTO uploadDriverAuthImage(ReqDriverAuthImageDTO reqDriverAuthImageDTO) {

		ResDriverAuthImageDTO resDriverAuthImageDTO = new ResDriverAuthImageDTO();

		String imgName = new ImageUpload().uploadWebPicture(reqDriverAuthImageDTO.getUploadPictures());
		if (ToolUtil.isNotBlank(imgName)) {

			resDriverAuthImageDTO.setImageName(imgName);
			resDriverAuthImageDTO.setImageUrl(ToolUtil.imageAddress(imgName));

			return resDriverAuthImageDTO;
		} else {
			log.error("上传车辆图片的时候出错！UserOperateFacadeImpl : uploadDriverAuthImage()");
			throw new BussinessException(BizExpEnum.FAILED_UPLOAD_IMG);
		}

	}
}
